
let spwanColors = function (number, exclude) {
 let colorsArr = [] 
 let colorHslArr = []
 // 获取颜色的hsl值
 let excludeHsl = rgbToHsl(parseInt(exclude.substring(1, 3)),parseInt(exclude.substring(3, 5)),parseInt(exclude.substring(5, 7)))
 // 批量生成颜色值
 for (let i = 0; i < number; i++) {
  // 获取随机的颜色值
  var r = Math.floor(Math.random()*256);
  var g = Math.floor(Math.random()*256);
  var b = Math.floor(Math.random() * 256);
  // 获取当前颜色的hsl值
  let currentHsl = rgbToHsl(r, g, b)
  // 色相相差0.25为相似颜色
  if (hslCheck(currentHsl,excludeHsl)) { 
   i--;
   continue;
  }
  let isExclude = false
  // 循环查看
  for (let j = 0; j < colorHslArr.length; j++) { 
   let hslItem = colorHslArr[j]
   
   if (hslCheck(currentHsl,hslItem)) { 

    isExclude = true
    break;
   }
  }
  if (isExclude) {
   i--;
   continue;
  } else { 
   colorHslArr.push(currentHsl)
  }
  
  let rHex = checkDouble(r.toString(16))
  let gHex = checkDouble(g.toString(16))
  let bHex = checkDouble(b.toString(16))
  console.error('当前颜色值数量为',colorsArr.length)
  colorsArr.push("#"+rHex+gHex+bHex)
 }

 console.error(colorsArr)
}

function hslCheck(hsl,hsl2) {
    let hslNumber = hsl[0]+hsl[1]+hsl[2]
    let hsl2Number = hsl2[0] + hsl2[1] + hsl2[2]
    if (Math.abs(hslNumber - hsl2Number) < 0.1) { 
        return false
    }
    return true
}
function rgbToHsl(r, g, b) {
 r /= 255, g /= 255, b /= 255;
 var max = Math.max(r, g, b), min = Math.min(r, g, b);
 var h, s, l = (max + min) / 2;

 if (max == min){ 
     h = s = 0; // achromatic
 } else {
     var d = max - min;
     s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
     switch(max) {
         case r: h = (g - b) / d + (g < b ? 6 : 0); break;
         case g: h = (b - r) / d + 2; break;
         case b: h = (r - g) / d + 4; break;
     }
     h /= 6;
 }

 return [h, s, l];
}

let checkDouble = function (val) {
 if (val.length == 1) { 
  return '0'+val
 }
 return val
}
 
spwanColors(30,'#0B205B')



